package com.ruoyi.sales.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.sales.domain.*;
import com.ruoyi.sales.service.*;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 成品仓库Controller
 * 
 * @author woo
 * @date 2024-04-12
 */
@RestController
@RequestMapping("/sales/storeProduct")
public class ZcStoreProductController extends BaseController
{
    @Autowired
    private IZcStoreProductService zcStoreProductService;
    @Autowired
    private IZcStoreInfoService storeInfoService;
    @Autowired
    private IZcProductService productService;
    @Autowired
    private IZcStoreEnterService storeEnterService;
    @Autowired
    private IZcSpecificationsService specificationsService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询成品仓库列表
     */
    @PreAuthorize("@ss.hasPermi('sales:storeProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZcStoreProduct zcStoreProduct)
    {
        startPage();
        List<ZcStoreProduct> list = zcStoreProductService.selectZcStoreProductList(zcStoreProduct);
        for (ZcStoreProduct storeProduct : list) {
            pageDisplay(storeProduct);
        }
        return getDataTable(list);
    }

    private void pageDisplay(ZcStoreProduct storeProduct) {
        ZcStoreInfo zcStoreInfo = storeInfoService.selectZcStoreInfoByStoreId(storeProduct.getStoreId());
        storeProduct.setStoreName(zcStoreInfo.getStoreName());
        ZcStoreEnter zcStoreEnter = storeEnterService.selectZcStoreEnterByEnterId(storeProduct.getEnterId());
        //拿到对应的产品
        ZcProduct zcProduct = productService.selectZcProductByProductId(zcStoreEnter.getProductId());
        //产品编码（产品条码）
        storeProduct.setProductCode(zcProduct.getCode());
        //产品名称
        storeProduct.setProductName(zcProduct.getName());
        //产品性质
        String nature = dictDataService.selectDictLabel("zc_product_nature", zcProduct.getNature());
        storeProduct.setProductNature(nature);
        //产品规格
        ZcSpecifications zcSpecifications = specificationsService.selectZcSpecificationsBySpecificationsId(zcProduct.getSpecificationsId());
        String specifications = dictDataService.selectDictLabel("zc_specifications", String.valueOf(zcSpecifications.getWide()));
        storeProduct.setProductSpec("幅宽"+Integer.valueOf(specifications)+",克重"+zcSpecifications.getWeight());
        //拿到对应产品米数
        storeProduct.setProductMeters(zcStoreEnter.getMeters());
    }

    /**
     * 导出成品仓库列表
     */
    @PreAuthorize("@ss.hasPermi('sales:storeProduct:export')")
    @Log(title = "成品仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZcStoreProduct zcStoreProduct)
    {
        List<ZcStoreProduct> list = zcStoreProductService.selectZcStoreProductList(zcStoreProduct);
        ExcelUtil<ZcStoreProduct> util = new ExcelUtil<ZcStoreProduct>(ZcStoreProduct.class);
        util.exportExcel(response, list, "成品仓库数据");
    }

    /**
     * 获取成品仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:storeProduct:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zcStoreProductService.selectZcStoreProductById(id));
    }

    /**
     * 新增成品仓库
     */
    @PreAuthorize("@ss.hasPermi('sales:storeProduct:add')")
    @Log(title = "成品仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZcStoreProduct zcStoreProduct)
    {
        return toAjax(zcStoreProductService.insertZcStoreProduct(zcStoreProduct));
    }

    /**
     * 修改成品仓库
     */
    @PreAuthorize("@ss.hasPermi('sales:storeProduct:edit')")
    @Log(title = "成品仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZcStoreProduct zcStoreProduct)
    {
        return toAjax(zcStoreProductService.updateZcStoreProduct(zcStoreProduct));
    }

    /**
     * 删除成品仓库
     */
    @PreAuthorize("@ss.hasPermi('sales:storeProduct:remove')")
    @Log(title = "成品仓库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zcStoreProductService.deleteZcStoreProductByIds(ids));
    }
}
